/*
Java source file
Copyright: Umschalt.com 2012
*/
package com.david.documentation.service.doku;

import com.david.documentation.domain.dao.category.CategoryDao;
import com.david.documentation.domain.dao.doku.DokuDao;
import com.david.documentation.domain.dao.tags.TagDao;
import com.david.documentation.domain.entities.category.Category;
import com.david.documentation.domain.entities.doku.Doku;
import com.david.documentation.domain.entities.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;

@Service(value = "dokuService")
@Transactional(rollbackFor = Exception.class)
public class DokuServiceImpl implements DokuService {

	@Autowired
	private DokuDao dokuDao;

	@Autowired
	private TagDao tagDao;

	@Autowired
	private CategoryDao categoryDao;

	
	public Doku findById(Long id) {
		return dokuDao.findById(id);
	}

	public void save(Doku doku) {
		dokuDao.merge(doku);
	}

	public Doku update(Doku doku) {
		return dokuDao.merge(doku);
	}

	public void remove(Doku doku) {
		dokuDao.removeSafely(doku);
	}

	public long size() {
		return dokuDao.size();
	}

	public List<Doku> findAll() {
		return dokuDao.findAll();
	}

	public List<Doku> findDokusByCategory(String category, int start, int count) {
		Category cat = categoryDao.findCategoryByName(category);
		if (cat != null){
			return dokuDao.findDokusByCategory(cat,start,count);
		}
		return Collections.emptyList();
	}
	public List<Doku> findDokusByCategory(String category){
		return findDokusByCategory(category,0,0);
	}

	public List<Doku> findDokusByTag(String tag, int start, int count){
		Tag tg = tagDao.findTagByName(tag);
		if (tg != null){
			return dokuDao.findDokusByTag(tg, start, count);
		}
		return Collections.emptyList();
	}
	public List<Doku> findDokusByTag(String tag){
		return findDokusByTag(tag,0,0);
	}


	public List<Doku> findDokusToday(int start,int count) {
		return dokuDao.findDokusToday(start,count);
	}

}
